package com.cssy.xxkj.logic.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cssy.xxkj.basic.entity.BasicMaterialsTypeDO;
import com.cssy.xxkj.logic.entity.InOutBillDO;
import com.cssy.xxkj.logic.entity.vo.LogicProductionDispatchingDetailItemVO;
import com.cssy.xxkj.logic.entity.vo.LogicSalesDetailVO;
import com.cssy.xxkj.logic.entity.vo.LogicWarehouseEndMonthSumVO;
import com.cssy.xxkj.logic.entity.vo.UtilsEntityVO;
import com.cssy.xxkj.logic.service.IExcelExportService;
import com.cssy.xxkj.logic.util.ExcelUtils;
import com.cssy.xxkj.logic.util.OftenMapUtil;
import com.plat.helper.SearchConditions;
import com.plat.util.json.OutJsonGrid;

@Controller
@RequestMapping("/excel")
public class ExcelExportController {
	
	
	@Resource(name="excelExportService")
	private IExcelExportService excelExportService;
	
	@Resource(name="oftenMapUtil")
	private OftenMapUtil oftenMapUtil;
	@RequestMapping("/batchInExcel")
	public void batchInExce(HttpServletRequest request,
			HttpServletResponse response, Model model){
		try {
			List<InOutBillDO> inOutList=excelExportService.findBatchInExce(request);
			new ExcelUtils().toExcel(inOutList, "日期:date┋供应商:vendorName┋单据编号:bilCode┋领料单据号(入库单号):runningNum┋"
					+ "单据类型:billType┋仓库名称:storageName┋"
					+ "物料编码:materialCode┋物料名称:materialName┋PI号:piCode┋物料型号:materialModel┋"
					+ "物料规格:materialSpec┋物料颜色:materialColor┋物料材质:materialQuanlity┋系列:materialType┋"
					+ "单位:materialUnit┋客户:consumerName┋入库数量:inQuantity┋"
					+ "出库数量:outQuantity┋单价:materialPrice┋总金额:allMoney┋"
					+ "业务员:salesman┋行记录描述:decript┋单备注:remark┋出库PI:outPicode" , "多PI入库明细", response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/bomMaterialListExcel")
	public void bomMaterialListExcel(HttpServletRequest request,
			HttpServletResponse response, Model model){
		try {
			List<UtilsEntityVO> listVo= excelExportService.findBOMMaterialAll(request);
			new ExcelUtils().toExcel(listVo, "BOM编码:bomCode┋BOM名称:bomName┋"
					+ "BOM型号:bomModel┋BOM尺寸:bomSize┋BOM单位:bomUntl┋"
					+ "产品类型:bomProType┋产品编码:bomProCode┋产品名称:bomProName┋"
					+ "产品系列:bomProSeries┋产品颜色:bomProColor┋产品材质:bomProQuanlity┋"
					+ "物品编码:materialCode┋物品名称:materialName┋物品型号:materialModel┋"
					+ "物品尺寸:materialSpec┋物品颜色:materialColor┋物品材质:materialQuanlity┋"
					+ "物品单位:materialUtil┋数量:materialQuantity", "BOM物料导出", response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@RequestMapping("/bomProcessListExcel")
	public void bomProcessListExcel(HttpServletRequest request,
			HttpServletResponse response, Model model){
		try {
			List<UtilsEntityVO> listVo = excelExportService.findBOMProcessListAll(request);
			new ExcelUtils().toExcel(listVo, "BOM编码:bomCode┋BOM名称:bomName┋"
					+ "BOM型号:bomModel┋BOM尺寸:bomSize┋BOM单位:bomUntl┋"
					+ "产品类型:bomProType┋产品颜色:bomProColor┋产品材质:bomProQuanlity┋"
					+ "产品编码:bomProCode┋产品名称:bomProName┋"
					+ "产品系列:bomProSeries┋工序编码:bomProcessCode┋工序名称:bomProcessName┋"
					+ "工序车间:bomWorkshop┋工序单价:bomProcessPrice", "BOM工序导出", response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	@RequestMapping("/salesOrderExcel")
	public void salesOrderExcel(HttpServletRequest request,
			HttpServletResponse response, Model model){
		try {
			List<LogicSalesDetailVO> salesList=excelExportService.findSalesDetailExcel(request);
			for(LogicSalesDetailVO vo:salesList){
				if("1".equals(vo.getZd001SalesType())){
					vo.setZd001SalesType("普通");
				}else if("0".equals(vo.getZd001SalesType())){
					vo.setZd001SalesType("加单");
				}
				if("MT".equals(vo.getZd001SalesModel())){
					vo.setZd001SalesModel("床垫");
				}else if("BT".equals(vo.getZd001SalesModel())){
					vo.setZd001SalesModel("套床");
				}
			}
			new ExcelUtils().toExcel(salesList, "销售订单号:zd001SalesCode┋销售类型:zd001SalesType┋产品类型:zd001SalesModel┋"
					+ "pi编号:zd002PiCode┋客户名称:zd003CustName┋客户编码:zd003CustCode┋"
					+ "物料编码:zd004MaterCode┋物料名称:zd004MaterName┋规格型号:zd004MaterModel┋"
					+ "物料尺寸:zd004MaterSpec┋物料颜色:zd004MaterColor┋物料材质:zd004MaterQuanlity┋"
					+ "单位:zd004MaterUnit┋数量:zd008Quantity┋业务员:zd005Clerk┋"
					+ "建单日期:zd006OrderDate┋交货日期:zd007DeliveryDate┋描述:zd004MaterDescript┋" , "销售明细导出", response);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	
	@RequestMapping("/storageOutExcel")
	public void storageOutExcel(HttpServletRequest request,
			HttpServletResponse response, Model model){
		try {
			List<InOutBillDO> inOutList=excelExportService.findstorageOutAndItemByConditions(request);
			new ExcelUtils().toExcel(inOutList, "日期:date┋供应商:vendorName┋单据编号:bilCode┋领料单据号(入库单号):runningNum┋"
					+ "单据类型:billType┋仓库名称:storageName┋"
					+ "物料编码:materialCode┋物料名称:materialName┋PI号:piCode┋物料型号:materialModel┋"
					+ "物料规格:materialSpec┋物料颜色:materialColor┋物料材质:materialQuanlity┋系列:materialType┋"
					+ "单位:materialUnit┋客户:consumerName┋入库数量:inQuantity┋"
					+ "出库数量:outQuantity┋单价:materialPrice┋总金额:allMoney┋"
					+ "业务员:salesman┋行记录描述:decript┋单备注:remark┋出库PI:outPicode" , "成品出库明细", response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@RequestMapping("/materialOutExcel")
	public void materialOutExcel(HttpServletRequest request,
			HttpServletResponse response, Model model){
		try {
			List<InOutBillDO> inOutList=excelExportService.findMaterialOutAndItemByConditions(request);
			new ExcelUtils().toExcel(inOutList, "日期:date┋供应商:vendorName┋单据编号:bilCode┋领料单据号(入库单号):runningNum┋"
					+ "单据类型:billType┋仓库名称:storageName┋"
					+ "物料编码:materialCode┋物料名称:materialName┋PI号:piCode┋物料型号:materialModel┋"
					+ "物料规格:materialSpec┋物料颜色:materialColor┋物料材质:materialQuanlity┋系列:materialType┋"
					+ "单位:materialUnit┋客户:consumerName┋入库数量:inQuantity┋"
					+ "出库数量:outQuantity┋单价:materialPrice┋总金额:allMoney┋"
					+ "业务员:salesman┋行记录描述:decript┋单备注:remark┋出库PI:outPicode" , "领料出库明细", response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@RequestMapping("/endMonthExcel")
	public void endMonth(HttpServletRequest request,
			HttpServletResponse response, Model model){
		List<LogicWarehouseEndMonthSumVO> endMothSumList=excelExportService.findWarehouseEndMOthSumList(request);
/*		for(LogicWarehouseEndMonthSumVO le:endMothSumList){
			le.setZdAvgPrice((le.getZdAvgPrice()==null?BigDecimal.ZERO:le.getZdAvgPrice().setScale(2,BigDecimal.ROUND_UP)));//平均价
			le.setZdTaskQuantity(le.getZdTaskQuantity()==null?BigDecimal.ZERO:le.getZdTaskQuantity().setScale(2,BigDecimal.ROUND_UP));//期初数量
			le.setZdTaskSumMoney(le.getZdTaskSumMoney()==null?BigDecimal.ZERO:le.getZdTaskSumMoney().setScale(2,BigDecimal.ROUND_UP));//期初金额
			le.setZdInQuantity(le.getZdInQuantity()==null?BigDecimal.ZERO:le.getZdInQuantity().setScale(2,BigDecimal.ROUND_UP));//入库数量
			le.setZdInSumMoney(le.getZdInSumMoney()==null?BigDecimal.ZERO:le.getZdInSumMoney().setScale(2,BigDecimal.ROUND_UP));//入库金额
			le.setZdOutQuantity(le.getZdOutQuantity()==null?BigDecimal.ZERO:le.getZdOutQuantity().setScale(2,BigDecimal.ROUND_UP));//出库数量
			le.setZdOutSumMoney(le.getZdOutSumMoney()==null?BigDecimal.ZERO:le.getZdOutSumMoney().setScale(2,BigDecimal.ROUND_UP));//出库金额
			le.setZdStockQuantity(le.getZdStockQuantity()==null?BigDecimal.ZERO:le.getZdStockQuantity().setScale(2,BigDecimal.ROUND_UP));//期末数量
			le.setZdStockSumMoney(le.getZdStockSumMoney()==null?BigDecimal.ZERO:le.getZdStockSumMoney().setScale(2,BigDecimal.ROUND_UP));//期末金额
		}*/
		new ExcelUtils().toExcel(endMothSumList, "物料编码:zdMaterCode┋物料名称:zdMaterName┋规格型号:zdMaterModel┋"
					+ "规格尺寸:zdMaterSize┋颜色:zdMaterColor┋材质:zdMaterQuanlity┋"
					+ "类别:zdMaterTypeName┋单位:zdMaterUnit┋单价:zdAvgPrice┋"//
					+ "期初数量:zdTaskQuantity┋期初金额:zdTaskSumMoney┋入库数量:zdInQuantity┋"
					+ "入库金额:zdInSumMoney┋出库数量:zdOutQuantity┋出库金额:zdOutSumMoney┋"
					+ "期末数量:zdStockQuantity┋期末金额:zdStockSumMoney┋仓库名称:zdWhName┋"
					+ "单位名称:zdCompName┋系列:zdMaterSery┋PI编号:piCode", "期末库存汇总表", response);//
		
	}
	
	@RequestMapping("/otherOutExcel")
	private void otherOutExcel(HttpServletRequest request,
			HttpServletResponse response, Model model){
		try {
			List<InOutBillDO> inOutList=excelExportService.findOtherOutAndItemByConditions(request);
			new ExcelUtils().toExcel(inOutList, "日期:date┋供应商:vendorName┋单据编号:bilCode┋领料单据号(入库单号):runningNum┋"
					+ "单据类型:billType┋仓库名称:storageName┋"
					+ "物料编码:materialCode┋物料名称:materialName┋PI号:piCode┋物料型号:materialModel┋"
					+ "物料规格:materialSpec┋物料颜色:materialColor┋物料材质:materialQuanlity┋系列:materialType┋"
					+ "单位:materialUnit┋客户:consumerName┋入库数量:inQuantity┋"
					+ "出库数量:outQuantity┋单价:materialPrice┋总金额:allMoney┋"
					+ "业务员:salesman┋行记录描述:decript┋单备注:remark┋出库PI:outPicode" , "其他出库明细", response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@RequestMapping("/otherInExcel")
	public void otherInExcel(HttpServletRequest request,
			HttpServletResponse response, Model model){
		try {
			
			List<InOutBillDO> inOutList=excelExportService.findOtherInAndItemByConditions(request);
			
			new ExcelUtils().toExcel(inOutList, "日期:date┋供应商:vendorName┋单据编号:bilCode┋领料单据号(入库单号):runningNum┋"
					+ "单据类型:billType┋仓库名称:storageName┋"
					+ "物料编码:materialCode┋物料名称:materialName┋PI号:piCode┋物料型号:materialModel┋"
					+ "物料规格:materialSpec┋物料颜色:materialColor┋物料材质:materialQuanlity┋系列:materialType┋"
					+ "单位:materialUnit┋客户:consumerName┋入库数量:inQuantity┋"
					+ "出库数量:outQuantity┋单价:materialPrice┋总金额:allMoney┋业务员:salesman┋"
					+ "行记录描述:decript┋单备注:remark┋出库PI:outPicode" , "其他入库明细", response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/purchasewarenExcel")
	public void purchasewareIn(HttpServletRequest request,
			HttpServletResponse response, Model model){
		try {
			List<InOutBillDO> inOutList=excelExportService.findPurchasewareInAndItemByConditions(request);
			new ExcelUtils().toExcel(inOutList, "日期:date┋供应商:vendorName┋单据编号:bilCode┋领料单据号(入库单号):runningNum┋"
					+ "单据类型:billType┋仓库名称:storageName┋"
					+ "物料编码:materialCode┋物料名称:materialName┋PI号:piCode┋物料型号:materialModel┋"
					+ "物料规格:materialSpec┋物料颜色:materialColor┋物料材质:materialQuanlity┋系列:materialType┋"
					+ "单位:materialUnit┋客户:consumerName┋入库数量:inQuantity┋"
					+ "出库数量:outQuantity┋单价:materialPrice┋总金额:allMoney"
					+ "┋业务员:salesman┋行记录描述:decript┋单备注:remark┋出库PI:outPicode" , "采购入库明细", response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@RequestMapping("/storageInExcel")
	public void storageIn(HttpServletRequest request,
			HttpServletResponse response, Model model){
		try {
			List<InOutBillDO> inOutList=excelExportService.findSotrageInAndItemByConditions(request);
			new ExcelUtils().toExcel(inOutList, "日期:date┋供应商:vendorName┋单据编号:bilCode┋领料单据号(入库单号):runningNum┋"
					+ "单据类型:billType┋仓库名称:storageName┋"
					+ "物料编码:materialCode┋物料名称:materialName┋PI号:piCode┋物料型号:materialModel┋"
					+ "物料规格:materialSpec┋物料颜色:materialColor┋物料材质:materialQuanlity┋系列:materialType┋"
					+ "单位:materialUnit┋客户:consumerName┋入库数量:inQuantity┋"
					+ "出库数量:outQuantity┋单价:materialPrice┋总金额:allMoney┋"
					+ "业务员:salesman┋行记录描述:decript┋单备注:remark┋出库PI:outPicode" , "成品入库明细", response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/collectDispatchingDetailsItemExcel")
	public void collectDispatchingDetailsItemExcel(HttpServletRequest request,
			HttpServletResponse response, Model model){
		try {
			List<LogicProductionDispatchingDetailItemVO> LogicProductionDispatchingDetailItemList=excelExportService.findDispatchingDetatlsReports(request);
			new ExcelUtils().toExcel(LogicProductionDispatchingDetailItemList, "PI单号:zd003PiCode┋派工料单号:zd001FormCode┋"
					+ "客户名称:customerName┋产品编码:zd006MaterialsCode┋产品名称:zd006MaterialsName┋"
					+ "型号:zd010MaterialsModel┋尺寸:zd023MaterialsSize┋颜色:zd026MaterialsColor┋"
					+ "材质:zd027MaterialsQuality┋员工姓名:zd008UseName┋工序:zd029ProcessName┋"
					+ "车间:zd028WorkShopName┋派工数目:zdTheQuantity┋工序单价:zd014Price┋"
					+ "总价:allprice┋派工时间:bizCreatorDate┋日报时间:zd008WorkDate┋", "生产派工明细", response);
		
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@RequestMapping("/getEndMonthExcel")
	public void getEndMonthExcel(HttpServletRequest request,
			HttpServletResponse response, Model model){
		List<LogicWarehouseEndMonthSumVO> endMothSumList=excelExportService.findEndMonth(request);
		new ExcelUtils().toExcel(endMothSumList, "物料编码:zdMaterCode┋物料名称:zdMaterName┋规格型号:zdMaterModel┋"
				+ "规格尺寸:zdMaterSize┋颜色:zdMaterColor┋材质:zdMaterQuanlity┋"
				+ "类别:zdMaterTypeName┋单位:zdMaterUnit┋单价:zdAvgPrice┋"//
				+ "期初数量:zdTaskQuantity┋期初金额:zdTaskSumMoney┋入库数量:zdInQuantity┋"
				+ "入库金额:zdInSumMoney┋出库数量:zdOutQuantity┋出库金额:zdOutSumMoney┋"
				+ "期末数量:zdStockQuantity┋期末金额:zdStockSumMoney┋仓库名称:zdWhName┋"
				+ "期初单位:zdCompName┋入库单位:zdCompNameIn┋出库单位:zdCompNameOut┋期末单位:zdCompNameEnd┋系列:zdMaterSery┋PI编号:piCode┋出库PI编号:outPicode", "期末库存汇总表", response);//
	}
	
	@RequestMapping("/getMaterialSerise")
	public void getMaterialSerise(HttpServletRequest request,
			HttpServletResponse response, Model model){
		String cods=request.getParameter("codes");
		String[] cd=cods.split(",");
		List<String> params= new ArrayList<String>();
		boolean isOk=true;
		for(int i = 0 ; i < cd.length ; i++ ){
			for(String param:params){
				if(param.equals(cd[i])){
					isOk=false;
				}
			}
			if(isOk){
				params.add(cd[i]);
			}
			isOk=true;
		}
		Map<String , BasicMaterialsTypeDO> outMap=getMaterialTypeByCodeList(params);
		Map<String, String> out= new HashMap<String, String>();
		for(String key:outMap.keySet()){
			out.put(key, outMap.get(key)==null?"":outMap.get(key).getZd002Name());
		}
		JSONObject json = JSONObject.fromObject(out);
		OutJsonGrid outJson = new OutJsonGrid(json);
		outJson.printJSon(response);
	}
	private Map<String , BasicMaterialsTypeDO> getMaterialTypeByCodeList(List<String> params){
		int i=0;
		if(params.size()%100!=0){
			i=(params.size()/100) + 1;
		}else{
			i=params.size()/100;
		}
		Map<String , BasicMaterialsTypeDO> mtMap= new HashMap<String, BasicMaterialsTypeDO>();
		for(int j = 0 ; j < i ; j++ ){
			SearchConditions scs = new SearchConditions();
			int end=params.size();
			if(((j+1)*100-1)<params.size()){
				end=(j+1)*100;
			}
			scs.addCondition("zd001Code", params.subList(j*100, end));
			mtMap.putAll(this.oftenMapUtil.getMaterialTypeKeyMaterialCode(scs));
		}
		return mtMap;
	}
	
}
